home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / termSendText.c < prev    next >
C/C++ Source or Header  |  1995-06-19  |  9KB  |  577 lines

  1. /*
  2. **    termSendText.c
  3. **
  4. **    Text sending support routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termARexxGlobal.h"
  11.  
  12.     /* Local variables. */
  13.  
  14. STATIC LONG WaitCount,PromptCount;
  15.  
  16.     /* MatchPrompt():
  17.      *
  18.      *    Search incoming data stream for a match.
  19.      */
  20.  
  21. STATIC BYTE __regargs
  22. MatchPrompt(register STRPTR Data,register LONG Size,register STRPTR Prompt,register LONG PromptLen)
  23. {
  24.     register UBYTE c,Mask;
  25.  
  26.     if(Config -> SerialConfig -> StripBit8)
  27.         Mask = 0x7F;
  28.     else
  29.         Mask = 0xFF;
  30.  
  31.     do
  32.     {
  33.         if(c = ((*Data++) & Mask))
  34.         {
  35.             register BYTE MatchMade;
  36.  
  37.             do
  38.             {
  39.                 MatchMade = FALSE;
  40.  
  41.                 if(PromptCount == WaitCount)
  42.                 {
  43.                     if(c == Prompt[WaitCount] & Mask)
  44.                     {
  45.                         MatchMade = TRUE;
  46.  
  47.                         if(PromptLen == ++PromptCount)
  48.                             return(TRUE);
  49.                     }
  50.                 }
  51.  
  52.                 if(MatchMade)
  53.                     WaitCount++;
  54.                 else
  55.                 {
  56.                     if(WaitCount)
  57.                     {
  58.                         WaitCount = 0;
  59.  
  60.                         PromptCount = 0;
  61.                     }
  62.                     else
  63.                         break;
  64.                 }
  65.             }
  66.             while(!WaitCount);
  67.         }
  68.     }
  69.     while(--Size);
  70.  
  71.     return(FALSE);
  72. }
  73.  
  74.     /* WaitForPrompt(STRPTR Prompt,LONG PromptLen):
  75.      *
  76.      *    Scan the incoming data flow for a certain string.
  77.      */
  78.  
  79. STATIC BYTE __regargs
  80. WaitForPrompt(STRPTR Prompt,LONG PromptLen)
  81. {
  82.     ULONG Signals;
  83.  
  84.     WaitCount = PromptCount = 0;
  85.  
  86.     if(DataHold)
  87.     {
  88.         DataHold = NULL;
  89.  
  90.         RestartSerial(FALSE);
  91.     }
  92.  
  93.     if(CheckSerialRead())
  94.         Signals = SIG_SERIAL;
  95.     else
  96.         Signals = NULL;
  97.  
  98.     StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  99.  
  100.     for(;;)
  101.     {
  102.         if(Signals & SIG_SERIAL)
  103.         {
  104.             if(!WaitSerialRead())
  105.             {
  106.                 LONG Length;
  107.  
  108.                 BytesIn++;
  109.  
  110.                 if(Translate_CR_LF)
  111.                     Length = (* Translate_CR_LF)(ReadBuffer,1);
  112.                 else
  113.                     Length = 1;
  114.  
  115.                 if(Length)
  116.                 {
  117.                     ConProcess(ReadBuffer,Length);
  118.  
  119.                     Status = STATUS_READY;
  120.  
  121.                     if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  122.                     {
  123.                         if(!CheckIO(TimeRequest))
  124.                             AbortIO(TimeRequest);
  125.  
  126.                         WaitIO(TimeRequest);
  127.  
  128.                         RestartSerial(FALSE);
  129.  
  130.                         return(TRUE);
  131.                     }
  132.                 }
  133.  
  134.                 do
  135.                 {
  136.                         /* Check how many bytes are still in
  137.                          * the serial buffer.
  138.                          */
  139.  
  140.                     if(Length = GetSerialWaiting())
  141.                     {
  142.                         if(Length > SerialBufferSize)
  143.                             Length = SerialBufferSize;
  144.  
  145.                         if(!DoSerialRead(ReadBuffer,Length))
  146.                         {
  147.                             BytesIn += Length;
  148.  
  149.                             if(Translate_CR_LF)
  150.                                 Length = (* Translate_CR_LF)(ReadBuffer,Length);
  151.  
  152.                             if(Length)
  153.                             {
  154.                                 ConProcess(ReadBuffer,Length);
  155.  
  156.                                 Status = STATUS_READY;
  157.  
  158.                                 if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  159.                                 {
  160.                                     if(!CheckIO(TimeRequest))
  161.                                         AbortIO(TimeRequest);
  162.  
  163.                                     WaitIO(TimeRequest);
  164.  
  165.                                     RestartSerial(FALSE);
  166.  
  167.                                     return(TRUE);
  168.                                 }
  169.                             }
  170.                         }
  171.                     }
  172.                 }
  173.                 while(Length);
  174.             }
  175.  
  176.             RestartSerial(FALSE);
  177.         }
  178.  
  179.         if(Signals & SIG_TIMER)
  180.         {
  181.             WaitIO(TimeRequest);
  182.  
  183.             return(FALSE);
  184.         }
  185.  
  186.         Signals = Wait(SIG_SERIAL | SIG_TIMER);
  187.     }
  188. }
  189.  
  190.     /* SendLinePrompt(STRPTR Line,LONG Len):
  191.      *
  192.      *    Send text line, wait for prompt.
  193.      */
  194.  
  195. BYTE
  196. SendLinePrompt(STRPTR Line,LONG Len)
  197. {
  198.     LONG i;
  199.  
  200.     if(Len == -1)
  201.         Len = strlen(Line);
  202.  
  203.     while(Len)
  204.     {
  205.         i = 0;
  206.  
  207.         while(i < Len && Line[i] != '\r')
  208.             i++;
  209.  
  210.  
  211.         if(Line[i] == '\r')
  212.         {
  213.             i++;
  214.  
  215.             SerWrite(Line,i);
  216.  
  217.             if(!WaitForPrompt(SendPrompt,SendPromptLen))
  218.                 return(FALSE);
  219.         }
  220.         else
  221.         {
  222.             if(i)
  223.                 SerWrite(Line,i);
  224.         }
  225.  
  226.         Len    -= i;
  227.         Line    += i;
  228.     }
  229.  
  230.     return(TRUE);
  231. }
  232.  
  233.     /* SendLineSimple(STRPTR Line,LONG Len):
  234.      *
  235.      *    Send a text line, no fancy features.
  236.      */
  237.  
  238. BYTE
  239. SendLineSimple(STRPTR Line,LONG Len)
  240. {
  241.     if(Len == -1)
  242.         Len = strlen(Line);
  243.  
  244.     SerWrite(Line,Len);
  245.  
  246.     return(TRUE);
  247. }
  248.  
  249.     /* SendLineDial(STRPTR Line,LONG Len):
  250.      *
  251.      *    The SendLineSimple for the dialer and init commands.
  252.      */
  253.  
  254. BYTE
  255. SendLineDial(STRPTR Line,LONG Len)
  256. {
  257.     if(Len == -1)
  258.         Len = strlen(Line);
  259.  
  260.     if(Config -> ModemConfig -> CharSendDelay)
  261.     {
  262.         ULONG    Seconds    = Config -> ModemConfig -> CharSendDelay / MILLION,
  263.             Micros    = Config -> ModemConfig -> CharSendDelay % MILLION;
  264.  
  265.         while(Len--)
  266.         {
  267.             SerWrite(Line++,1);
  268.  
  269.             WaitTime(Seconds,Micros);
  270.         }
  271.     }
  272.     else
  273.         SerWrite(Line,Len);
  274.  
  275.     return(TRUE);
  276. }
  277.  
  278.     /* SendLineDelay(STRPTR Line,LONG Len):
  279.      *
  280.      *    Send a text line, include a delay where necessary.
  281.      */
  282.  
  283. BYTE
  284. SendLineDelay(STRPTR Line,LONG Len)
  285. {
  286.     if(Len == -1)
  287.         Len = strlen(Line);
  288.  
  289.     while(Len--)
  290.     {
  291.         SerWrite(Line,1);
  292.  
  293.         if(*Line == '\r')
  294.         {
  295.             if(Config -> ClipConfig -> LineDelay)
  296.                 WaitTime(Config -> ClipConfig -> LineDelay / 100,(Config -> ClipConfig -> LineDelay % 100) * 10000);
  297.         }
  298.         else
  299.         {
  300.             if(Config -> ClipConfig -> CharDelay)
  301.                 WaitTime(Config -> ClipConfig -> CharDelay / 100,(Config -> ClipConfig -> CharDelay % 100) * 10000);
  302.         }
  303.  
  304.         Line++;
  305.     }
  306.  
  307.     return(TRUE);
  308. }
  309.  
  310.     /* SendLineEcho(STRPTR Line,LONG Len):
  311.      *
  312.      *    Send a text line, wait for single characters to be echoed.
  313.      */
  314.  
  315. BYTE
  316. SendLineEcho(STRPTR Line,LONG Len)
  317. {
  318.     ULONG Signals;
  319.  
  320.     if(DataHold)
  321.     {
  322.         DataHold = NULL;
  323.  
  324.         RestartSerial(FALSE);
  325.     }
  326.  
  327.     if(Len == -1)
  328.         Len = strlen(Line);
  329.  
  330.     while(Len--)
  331.     {
  332.         SerWrite(Line,1);
  333.  
  334.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  335.  
  336.         FOREVER
  337.         {
  338.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  339.  
  340.             if(Signals & SIG_SERIAL)
  341.             {
  342.                 if(!WaitSerialRead())
  343.                 {
  344.                     LONG Length;
  345.  
  346.                     if(Translate_CR_LF)
  347.                         Length = (* Translate_CR_LF)(ReadBuffer,1);
  348.                     else
  349.                         Length = 1;
  350.  
  351.                     if(Length > Len)
  352.                         Length = Len;
  353.  
  354.                     if(Length)
  355.                     {
  356.                         if(!memcmp(ReadBuffer,Line,Length))
  357.                         {
  358.                             if(!CheckIO(TimeRequest))
  359.                                 AbortIO(TimeRequest);
  360.  
  361.                             WaitIO(TimeRequest);
  362.  
  363.                             RestartSerial(FALSE);
  364.  
  365.                             break;
  366.                         }
  367.                     }
  368.  
  369.                     RestartSerial(FALSE);
  370.                 }
  371.                 else
  372.                 {
  373.                     if(!CheckIO(TimeRequest))
  374.                         AbortIO(TimeRequest);
  375.  
  376.                     WaitIO(TimeRequest);
  377.  
  378.                     RestartSerial(FALSE);
  379.  
  380.                     return(FALSE);
  381.                 }
  382.             }
  383.  
  384.             if(Signals & SIG_TIMER)
  385.             {
  386.                 WaitIO(TimeRequest);
  387.  
  388.                 return(FALSE);
  389.             }
  390.         }
  391.  
  392.         Line++;
  393.     }
  394.  
  395.     return(TRUE);
  396. }
  397.  
  398.     /* SendLineAnyEcho(STRPTR Line,LONG Len):
  399.      *
  400.      *    Send a text line, wait for characters to be echoed.
  401.      */
  402.  
  403. BYTE
  404. SendLineAnyEcho(STRPTR Line,LONG Len)
  405. {
  406.     ULONG Signals;
  407.  
  408.     if(DataHold)
  409.     {
  410.         DataHold = NULL;
  411.  
  412.         RestartSerial(FALSE);
  413.     }
  414.  
  415.     if(Len == -1)
  416.         Len = strlen(Line);
  417.  
  418.     while(Len--)
  419.     {
  420.         SerWrite(Line,1);
  421.  
  422.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  423.  
  424.         FOREVER
  425.         {
  426.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  427.  
  428.             if(Signals & SIG_SERIAL)
  429.             {
  430.                 if(!WaitSerialRead())
  431.                 {
  432.                     if(!CheckIO(TimeRequest))
  433.                         AbortIO(TimeRequest);
  434.  
  435.                     WaitIO(TimeRequest);
  436.  
  437.                     RestartSerial(FALSE);
  438.  
  439.                     break;
  440.                 }
  441.                 else
  442.                 {
  443.                     if(!CheckIO(TimeRequest))
  444.                         AbortIO(TimeRequest);
  445.  
  446.                     WaitIO(TimeRequest);
  447.  
  448.                     RestartSerial(FALSE);
  449.  
  450.                     return(FALSE);
  451.                 }
  452.             }
  453.  
  454.             if(Signals & SIG_TIMER)
  455.             {
  456.                 WaitIO(TimeRequest);
  457.  
  458.                 return(FALSE);
  459.             }
  460.         }
  461.  
  462.         Line++;
  463.     }
  464.  
  465.     return(TRUE);
  466. }
  467.  
  468.     /* SendLineKeyDelay(STRPTR Line,LONG Len):
  469.      *
  470.      *    Send a text line, include keyboard delay pauses between characters.
  471.      */
  472.  
  473. BYTE
  474. SendLineKeyDelay(STRPTR Line,LONG Len)
  475. {
  476.     struct Preferences Prefs;
  477.  
  478.     if(Len == -1)
  479.         Len = strlen(Line);
  480.  
  481.         /* Get current key repeat delay. */
  482.  
  483.     GetPrefs(&Prefs,offsetof(struct Preferences,KeyRptDelay));
  484.  
  485.         /* Any delay specified at all? */
  486.  
  487.     if(Prefs . KeyRptSpeed . tv_secs || Prefs . KeyRptSpeed . tv_micro)
  488.     {
  489.         while(Len--)
  490.         {
  491.             SerWrite(Line++,1);
  492.  
  493.             if(Len)
  494.             {
  495.                 TimeRequest -> tr_node . io_Command    = TR_ADDREQUEST;
  496.                 TimeRequest -> tr_time            = Prefs . KeyRptSpeed;
  497.  
  498.                 DoIO(TimeRequest);
  499.             }
  500.         }
  501.     }
  502.     else
  503.         SerWrite(Line,Len);
  504.  
  505.     return(TRUE);
  506. }
  507.  
  508.     /* SendSetup():
  509.      *
  510.      *    Choose the right routine for the text line output job.
  511.      */
  512.  
  513. VOID
  514. SendSetup()
  515. {
  516.         /* Prepare the prompt string. */
  517.  
  518.     if(Config -> ClipConfig -> LinePrompt[0])
  519.         SendPromptLen = TranslateString(Config -> ClipConfig -> LinePrompt,SendPrompt);
  520.     else
  521.     {
  522.         SendPrompt[0] = 0;
  523.         SendPromptLen = 0;
  524.     }
  525.  
  526.         /* Pick the line send routine. */
  527.  
  528.     switch(Config -> ClipConfig -> PacingMode)
  529.     {
  530.         case PACE_DIRECT:
  531.  
  532.             SendLine = SendLineSimple;
  533.             break;
  534.  
  535.         case PACE_ECHO:
  536.  
  537.             if(Config -> ClipConfig -> SendTimeout)
  538.                 SendLine = SendLineEcho;
  539.             else
  540.                 SendLine = SendLineSimple;
  541.  
  542.             break;
  543.  
  544.         case PACE_ANYECHO:
  545.  
  546.             if(Config -> ClipConfig -> SendTimeout)
  547.                 SendLine = SendLineAnyEcho;
  548.             else
  549.                 SendLine = SendLineSimple;
  550.  
  551.             break;
  552.  
  553.         case PACE_PROMPT:
  554.  
  555.             if(Config -> ClipConfig -> SendTimeout)
  556.                 SendLine = SendLinePrompt;
  557.             else
  558.                 SendLine = SendLineSimple;
  559.  
  560.             break;
  561.  
  562.         case PACE_DELAY:
  563.  
  564.             if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  565.                 SendLine = SendLineDelay;
  566.             else
  567.                 SendLine = SendLineSimple;
  568.  
  569.             break;
  570.  
  571.         case PACE_KEYBOARD:
  572.  
  573.             SendLine = SendLineKeyDelay;
  574.             break;
  575.     }
  576. }
  577.